import 'package:efood_multivendor/controller/auth_controller.dart';
import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/controller/user_controller.dart';
import 'package:efood_multivendor/data/model/response/userinfo_model.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/helper/route_helper.dart';
import 'package:efood_multivendor/view/base/custom_app_bar.dart';
import 'package:efood_multivendor/view/screens/language/widget/language_widget.dart';
import 'package:flutter/material.dart';
import 'package:efood_multivendor/controller/localization_controller.dart';
import 'package:efood_multivendor/util/app_constants.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/view/base/custom_button.dart';
import 'package:efood_multivendor/view/base/custom_snackbar.dart';
import 'package:get/get.dart';

class ChooseLanguageScreen extends StatelessWidget {
  final bool fromMenu;
  ChooseLanguageScreen({this.fromMenu = false});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LocalizationController>(
        builder: (localizationController) {
      return Scaffold(
        appBar: (fromMenu || ResponsiveHelper.isDesktop(context))
            ? CustomAppBar(
                title: Get.find<LocalizationController>().selectedIndex == 0
                    ? "Language"
                    : Get.find<LocalizationController>().selectedIndex == 1
                        ? "ቋንቋ"
                        : "语言",
                isBackButtonExist: true)
            : null,
        body: SafeArea(
            child: Column(children: [
          Expanded(
              child: Scrollbar(
            child: SingleChildScrollView(
              physics: BouncingScrollPhysics(),
              padding: EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
              child: Center(
                  child: SizedBox(
                width: Dimensions.WEB_MAX_WIDTH,
                child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: EdgeInsets.symmetric(
                            horizontal:
                                Dimensions.PADDING_SIZE_EXTRA_SMALL * 3),
                        child: Text(
                            localizationController.selectedIndex == 0
                                ? "Select language"
                                : localizationController.selectedIndex == 1
                                    ? "ቋንቋ ይምረጡ"
                                    : "选择语言",
                            style: TextStyle(
                                fontSize: 18, fontWeight: FontWeight.bold)),
                      ),
                      SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
                      ListView.builder(
                        itemCount: localizationController.languages.length,
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemBuilder: (context, index) => LanguageWidget(
                          languageModel:
                              localizationController.languages[index],
                          localizationController: localizationController,
                          index: index,
                        ),
                      ),
                      SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
                    ]),
              )),
            ),
          )),
          !Get.find<UserController>().isLoading
              ? CustomButton(
                  buttonText: localizationController.selectedIndex == 0
                      ? "Save & Continue"
                      : localizationController.selectedIndex == 1
                          ? "መዝግብ እና ቀጥል"
                          : "保存并继续",
                  margin: EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
                  radius: Dimensions.RADIUS_SMALL * 2.1,
                  fontSize: Dimensions.fontSizeSmall * 1.5,
                  onPressed: () async {
                    if (localizationController.languages.length > 0 &&
                        localizationController.selectedIndex != -1) {
                      localizationController.setLanguage(Locale(
                        AppConstants
                                .languages[localizationController.selectedIndex]
                                .languageCode ??
                            'en',
                        AppConstants
                            .languages[localizationController.selectedIndex]
                            .countryCode,
                      ));

                      if (Get.find<UserController>().userInfoModel != null) {
                        UserInfoModel _userInfo = UserInfoModel(
                            fName:
                                Get.find<UserController>().userInfoModel!.fName,
                            birthDate: Get.find<UserController>()
                                .userInfoModel!
                                .birthDate,
                            appLanguage: AppConstants
                                .languages[localizationController.selectedIndex]
                                .languageCode);
                        await Get.find<UserController>().updateUserInfo(
                            _userInfo,
                            Get.find<AuthController>().getUserToken());
                      }

                      if (fromMenu) {
                        Navigator.pop(context);
                      } else {
                        Get.find<SplashController>().disableIntro();
                        Get.offNamed(
                            RouteHelper.getSignInRoute(RouteHelper.onBoarding));
                      }
                    } else {
                      showCustomSnackBar('select_a_language'.tr);
                    }
                  },
                )
              : Center(
                  child: CircularProgressIndicator.adaptive(
                    backgroundColor: Theme.of(context).colorScheme.secondary,
                  ),
                ),
        ])),
      );
    });
  }
}
